<template>
  <!-- 根容器：添加左右留白 -->
  <div class="shop-container">
    <!-- 返回按钮 -->
    <button class="back-btn" @click="goBack">← 返回</button>

    <!-- 店铺信息区 -->
    <div class="shop-header">
      <div class="shop-meta">
        <img class="shop-avatar" :src="merchantInfo.dept.businessImg" alt="店铺头像" />
        <div class="shop-info">
          <div class="shop-name">
            {{ merchantInfo.dept.name }}
            <span class="official-tag">官方</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 搜索区 -->
    <div class="search-bar">
      <input
        v-model="searchKeyword"
        type="text"
        placeholder="请输入你感兴趣的商品"
        class="search-input"
      />
      <button class="search-btn" @click="handleSearch">搜索</button>
    </div>

    <!-- 商品列表区 -->
    <div class="product-list">
      <div
        class="product-card"
        v-for="(product, idx) in filteredProducts"
        :key="product.productId || idx"
        @click="goToProductDetail(product.productId)"
      >
        <img class="product-img" :src="product.mainImage" :alt="product.productName" />
        <div class="product-name">{{ product.productName }}</div>
        <div class="price-sales">
          <div class="product-price">¥{{ product.price }}</div>
          <div class="product-sales">已售 {{ product.sale || 0 }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import axios from 'axios'
import { useRoute, useRouter } from 'vue-router'

const router = useRouter()
const route = useRoute()
const sellerUserId = route.params.sellerUserId // 从路由获取商家ID
const BASE_URL = 'http://localhost:9527/mall'

// 店铺数据
const shopInfo = ref({
  follower: 0,
})
const isFollowed = ref(false)

// 商品数据
const products = ref([
  {
    productId: 0,
    sellerUserId: 0,
    productName: '',
    price: 0.0,
    mainImage: '',
    sale: 0,
  },
])

// 店铺数据
const merchantInfo = ref({
  userId: 0,
  username: '',
  nickname: null,
  password: '',
  email: '',
  phone: '',
  filePath: null,
  forcePasswordChange: null,
  deleted: 0,
  deptId: 0,
  dept: {
    id: 0,
    name: '',
    code: '',
    contactPhone: '',
    address: '',
    businessLicense: '',
    status: 0,
    deleted: 0,
    businessImg: '',
  },
})

// 搜索关键词
const searchKeyword = ref('')

// 过滤商品列表
const filteredProducts = computed(() => {
  return products.value.filter((item) => item.productName.includes(searchKeyword.value))
})

// 关注状态切换
const toggleFollow = () => {
  isFollowed.value = !isFollowed.value
  // 实际项目中添加关注接口调用
}

// 搜索处理
const handleSearch = () => {
  console.log('搜索:', searchKeyword.value)
  // 可添加搜索接口调用
}

// 获取商家商品列表
const fetchProductData = async () => {
  try {
    const response = await axios.get(`${BASE_URL}/product/seller/${sellerUserId}`)
    if (response.data.code === '00000') {
      products.value = response.data.data
    }
    try {
      const response = await axios.get(`${BASE_URL}/user/${sellerUserId}`)
      if (response.data.code === '00000') {
        console.log('店铺信息', response.data.data)
        merchantInfo.value = response.data.data
      }
    } catch (err) {
      console.error('店铺详情接口调用失败：', err)
    }
  } catch (err) {
    console.error('获取商品失败：', err)
  }
}

// 跳转到商品详情页
const goToProductDetail = (productId) => {
  if (!productId) {
    message.error('商品ID为空,无法跳转')
    return
  }
  router.push({
    name: 'detailShop',
    params: { goodsId: productId }, // 传递商品ID
  })
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 页面加载时获取数据
onMounted(() => {
  fetchProductData()
})
</script>

<style scoped>
/* 根容器：限制宽度 + 左右留白 + 居中 */
.shop-container {
  max-width: 1200px; /* 内容最大宽度 */
  margin: 0 auto; /* 水平居中 */
  padding: 0 20px; /* 左右留白 */
  font-family: 'Microsoft YaHei', sans-serif;
}

/* 返回按钮 */
.back-btn {
  color: #666;
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 14px;
  padding: 12px 0;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.back-btn:hover {
  color: #333;
}

/* 店铺信息区（添加阴影） */
.shop-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
  background: #fff;
  border-bottom: 1px solid #eee;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05); /* 新增阴影效果 */
}
.shop-meta {
  display: flex;
  align-items: center;
  gap: 12px;
}
.shop-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
}
.shop-name {
  font-size: 16px;
  font-weight: 600;
}
.official-tag {
  background: #4caf50;
  color: #fff;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
  margin-left: 6px;
}

/* 搜索区（搜索框缩短为一半） */
.search-bar {
  display: flex;
  gap: 8px;
  padding: 12px;
  margin: 16px 0;
  background: #f8f8f8;
  border-radius: 8px;
}
.search-input {
  flex: 0 0 50%; /* 搜索框宽度缩短为一半 */
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  outline: none;
}
.search-input:focus {
  border-color: #007bff;
}
.search-btn {
  background: #ff6700;
  color: #fff;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  border: none;
}
.search-btn:hover {
  opacity: 0.9;
}

/* 商品列表区 */
.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin: 16px 0;
}
.product-card {
  background: #fff;
  border-radius: 8px;
  padding: 12px;
  text-align: center;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: transform 0.2s;
}
.product-card:hover {
  transform: translateY(-2px);
}
.product-img {
  width: 100%;
  height: 150px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 8px;
}
.product-name {
  font-size: 14px;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 4px;
}

/* 价格和销量布局（左右分布） */
.price-sales {
  display: flex;
  justify-content: space-between;
  width: 100%;
  margin-top: 4px;
}
.product-price {
  color: #ff6700;
  font-weight: 600;
}
.product-sales {
  font-size: 12px;
  color: #999;
}
</style>
